package com.chengccn.aop.aoptest.aspect;

import com.chengccn.aop.aoptest.annontation.SysLog;
import com.google.gson.Gson;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

/**
 * @Author: Administrator
 * @Description: 日志测试注解,切面处理类
 * @Date: 2019/5/15 10:29
 * @Version: 1.0
 */
@Aspect
@Component
public class SysLogAspect {
    private final Logger log = LoggerFactory.getLogger(SysLogAspect.class);

    /**
     * 获取切点 这里用注解作为切点
     */
    @Pointcut("@annotation(com.chengccn.aop.aoptest.annontation.SysLog)")
    public void logPointCut(){
        System.out.println("获取切点");
    }

    /**
     * 在切点之前执行
     */
    @Before("logPointCut()")
    public void before(){
        System.out.println("before执行了");
    }

    /**
     * 环绕执行
     * @param joinPoint 切点
     * @return 切点
     * @throws Throwable
     */
    @Around("logPointCut()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("around执行了");
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        SysLog syslog = method.getAnnotation(SysLog.class);
        System.out.println(syslog);
        //请求的方法名
        String className = joinPoint.getTarget().getClass().getName();
        String methodName = signature.getName();
        Object[] args = joinPoint.getArgs();
        String params = new Gson().toJson(args);
        System.out.println(params);
        Object result = joinPoint.proceed();
        return result;
    }

    /**
     * 在切点后执行
     */
    @After("logPointCut()")
    public void after(){
        System.out.println("after执行了");
    }
}
